package com.melodyframe.stage.core.conductor;

import java.util.ArrayList;
import java.util.Collection;

import com.melodyframe.stage.core.CommonStage;
import com.melodyframe.stage.core.channel.Channel;
import com.melodyframe.stage.core.channel.CommonChannel;
import com.melodyframe.stage.core.performer.Performer;

public class CommonConductor implements Conductor<CommonStage> {

	private String	defaultPerformer;

	@Override
	public Channel createChannel(String path, CommonStage stage) {
		Collection<Performer> performerList = new ArrayList<Performer>();
		if (null != stage.getFilters()) {
			performerList.addAll(stage.getFilters());
		}
		Performer performer = this.getPerformer(path, stage);
		if (null != performer) {
			performerList.add(performer);

		}
		if (performerList.isEmpty()) {
			return null;
		} else {
			return new CommonChannel(performerList.iterator());
		}
	}

	public Performer getPerformer(String path, CommonStage stage) {
		if (stage.getPerformers().containsKey(path)) {
			return stage.getPerformers().get(path);
		} else if (stage.getPerformers().containsKey(this.defaultPerformer)) {
			return stage.getPerformers().get(this.defaultPerformer);
		} else {
			return null;
		}
	}

	public String getDefaultPerformer() {
		return defaultPerformer;
	}

	public void setDefaultPerformer(String defaultPerformer) {
		this.defaultPerformer = defaultPerformer;
	}

}
